package ink.sunflowerk.controller;


import ink.sunflowerk.pojo.PagerBean;
import ink.sunflowerk.pojo.Student;
import ink.sunflowerk.service.StudentService;
import ink.sunflowerk.util.ExcelCreateUtil;
import org.jboss.jandex.Index;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.server.UID;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/student")
public class StudentAction {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/show")
    public String show(Integer pageNum, HttpSession session) {
        if (pageNum == null) {
            pageNum = 1;
        }
//        List<Student> student = studentService.getAll();
        PagerBean<Student> pageBean = studentService.getPageBean(pageNum);
        session.setAttribute("pageBean", pageBean);
        System.out.println(pageBean);
        return "show";
    }

    @RequestMapping("/add")
    public String addStudent(Student student, Integer tid, Integer pageNum, HttpServletRequest request) {
        student.settId(tid);
        Integer i = studentService.addOne(student);
        request.setAttribute("message", "添加学生成功");

        return "forward:/student/show?pageNum=" + pageNum;
    }

    @RequestMapping("/get/{id}")
    public String get(@PathVariable("id") Integer id, Integer pageNum, HttpServletRequest request) {
        Student student = studentService.getOneByKey(id);
        request.setAttribute("student", student);
        request.setAttribute("pageNum", pageNum);
        return "update";
    }

    @RequestMapping("/update")
    public String update(Integer pageNum, Student student, HttpServletRequest request) {
        int n = studentService.updateOne(student);
        request.setAttribute("message", n > 0 ? "修改成功" : "修改失败");
        return "forward:/student/show?pageNum=" + pageNum;
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id, Integer pageNum, HttpServletRequest request) {
        Integer n = studentService.deleteOne(id);
        request.setAttribute("message", n > 0 ? "删除成功" : "删除失败");
        return "forward:/student/show?pageNum=" + pageNum;
    }

    //下载 excexcel
    @RequestMapping("dowloadExcel")
    public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws Exception {
        List<Student> list = studentService.getAll();
        ExcelCreateUtil.mapPut(Student.class, list);
        byte[] excel = ExcelCreateUtil.createExcel();
        String type = session.getServletContext().getMimeType(".xls");
        //创建HttpHeaders对象设置响应头信息
        MultiValueMap<String, String> headers = new HttpHeaders();
        //设置要下载方式以及下载文件的名字【只有 filename=?可以更改】
        headers.add("Content-Type", type);
        headers.add("Content-Length", excel.length + "");
        headers.add("Content-Disposition", "attachment;filename=" + UUID.randomUUID().toString() + ".xls");
        //设置响应状态码
        HttpStatus statusCode = HttpStatus.OK;
        //创建ResponseEntity对象
        //参数：bytes 响应体，headers 响应头，statusCode 状态码
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(excel, headers, statusCode);
        //关闭输入流
        return responseEntity;
    }
}
